<?php
namespace app\admin\controller;
use think\Db;
use app\common\controller\Backend;
class Ziyuan extends Backend{
    public function index(){

    }
	public function zlist(){
        $where=array();
        input('get.cate')?$where['cate']=input('get.cate'):'';
        input('get.model_id')?$where['model']=input('get.model_id'):'';
        input('get.gongnen_id')?$where['gongnen']=input('get.gongnen_id'):'';
        input('get.plate_id')?$where['plate']=input('get.plate_id'):'';
        input('get.page_id')?$where['page']=input('get.page_id'):'';
        input('get.home_admin')?$where['home_admin']=input('get.home_admin'):'';
        input('get.pro_id')?$where['filed_id']=input('get.pro_id'):'';
        $cate=Db::table('member_resource_cate')->select();
        $list=Db::table('member_resource')->where($where)->paginate(12);
        $this->assign('list',$list);
        $this->assign('cates',$cate);
		return $this->view->fetch();
	}

	public function zedit(){
        $id=input('get.id');
        $data=array();
        if($id){
            $data=Db::table('member_resource')->where('id', $id)->find();
            $data['file']=Db::table('member_resource_file')->where('r_id', $id)->select();
            $data['language']=explode(',',$data['language']);
        }
        $this->assign('data',$data);

        if(input('get.cid')){
            $gongnen=Db::table('member_gongnen')
                ->alias('a')
                ->join('member_gongnen_child b','a.id = b.gid','LEFT')
                ->where(['b.id'=>input('get.cid')])
                ->field('a.id,a.name,a.model_id,b.id as cid,b.cname')
                ->find();
            $this->assign('gongnen',$gongnen);
            $this->assign('cate_name','功能资源');
        }

       if(input('get.pid')){
            $page=Db::table('member_page')
                ->alias('a')
                ->join('member_plate b','a.plate_id = b.id','LEFT')
                ->where(['a.id'=>input('get.pid')])
                ->field('a.id,a.name,b.id as p_id,b.name as p_name,b.parent')
                ->find(); 
            $this->assign('page',$page);
            $this->assign('cate_name','页面资源');
        }


       if(input('get.mid')){
            $model=Db::table('member_model_manage')
                ->where(['id'=>input('get.mid')])
                ->field('id,name')
                ->find();
            $this->assign('model',$model);
            $this->assign('cate_name','模块资源');
        }

        if(input('get.plate_id')){
            $plate_check=Db::table('member_plate')
                ->where(['id'=>input('get.plate_id')])
                ->field('id,name')
                ->find();
            $this->assign('plate_check',$plate_check);
            $this->assign('cate_name','板块资源');
        }

        if(input('get.pro_id')){
            $project_check=Db::table('member_project')
                ->where(['id'=>input('get.pro_id')])
                ->field('id,name')
                ->find();
            $this->assign('project_check',$project_check);
            $this->assign('cate_name','项目资源');
        }




        $cate=Db::table('member_resource_cate')->select();
        $models=Db::table('member_model_manage')->select();
        $gongnens=Db::table('member_gongnen')->select();
        $child_gongens=Db::table('member_gongnen_child')->select();
        $plates=Db::table('member_plate')->select();
        $plate_tree=getTree($plates,0,0);

        $pages=Db::table('member_page')->select();

        $projects=Db::table('member_project')->select();

        //图片
        $attr['gnt']=Db::table('member_resource_attr')->where(['type'=>1])->select();
        //代码
        $attr['dm']=Db::table('member_resource_attr')->where(['type'=>2])->select();

        //前端
        $language['qd']=Db::table('member_technology_language')->where(['type'=>2])->select();
        //后端
        $language['hd']=Db::table('member_technology_language')->where(['type'=>1])->select();

        $this->assign('cates',$cate);
        $this->assign('models',$models);
        $this->assign('gongnens',$gongnens);
        $this->assign('child_gongens',$child_gongens);
        $this->assign('attr',$attr);
        $this->assign('language',$language);
        $this->assign('plates',$plate_tree);
        $this->assign('pages',$pages);
        $this->assign('projects',$projects);
		return $this->view->fetch();
	}

     public function zsave(){
        $data=$this->request->post();
        $path=empty($data['path'])?"":$data['path'];
        $des=empty($data['des'])?"":$data['des'];
        $fids=empty($data['fids'])?"":$data['fids'];
        $data['language']=empty($data['language'])?"":implode(",", $data['language']);

        unset($data['path']);
        unset($data['des']);
        unset($data['fids']);

        if(empty($data['id'])){
            $re=Db::table('member_resource')->insertGetId($data);
            if(!empty($path)){

               $arrs=array();
               foreach ($path as $key => $value) {
                $arr['path']= $value;
                $arr['des']=  $des[$key];
                $arr['r_id']= $re;
                array_push($arrs,$arr);
              } 
              Db::table('member_resource_file')->insertAll($arrs);
            }
        }else{
            $re=Db::table('member_resource')->where('id', $data['id'])->update($data);
           if(!empty($path)){ 
             foreach ($path as $key => $value){
                $arr['path']= $value;
                $arr['des']=  $des[$key];
                $arr['r_id']=$data['id'];
                if(empty($fids[$key])){
                    Db::table('member_resource_file')->insertGetId($arr);
                }else{
                    Db::table('member_resource_file')->where('id', $fids[$key])->update($data);
                }
             }

           }  
        }
        if($re!==false){
            $this->success(__('编辑成功'), 'Ziyuan/zlist');
        }else{
            $this->success(__('编辑失败'), 'Ziyuan/zlist');
        }
    }

    public function zdelete(){
        $id=input('get.id');
        $re=Db::table('member_resource')->where('id',$id)->delete();
        Db::table('member_resource_file')->where('r_id', $id)->delete();
        if($re){
            $this->success(__('删除成功'), 'Ziyuan/zlist');
        }else{
            $this->success(__('删除失败'), 'Ziyuan/zlist');
        }
    }

    public function changeStatus(){
        $id=input('get.id');
        $status=input('get.status');
        $re=Db::table('member_resource')->where('id',$id)->setField('status',$status);
        if($status==0){
            $success_msg="下线成功";
            $error_msg="下线成功";
        }else{
            $success_msg="上线成功";
            $error_msg="上线成功";
        }
        if($re){
            $this->success(__($success_msg), 'Ziyuan/zlist');
        }else{
            $this->success(__($error_msg), 'Ziyuan/zlist');
        }
    }


    //资源列表
    public function resourcefilelist(){
        $where=array();
        if(input('get.id')){
          $where['r_id']=input('get.id');
        }
        $list=Db::table('member_resource_file')->where($where)->paginate(16);
        $this->assign('list',$list);
        return $this->view->fetch();
    }


	//资源属性
	public function slist(){
        $list=Db::table('member_resource_attr')->paginate(12);
        $this->assign('list',$list);
        return $this->view->fetch();
	}
	public function sedit(){
        $id=input('get.id');
        $data=array();
        if($id){
            $data=Db::table('member_resource_attr')->where('id', $id)->find();
        }
        $this->assign('data',$data);
        return $this->view->fetch();
	}

    public function ssave(){
        $data=$this->request->post();
        if(empty($data['id'])){
            $re=Db::table('member_resource_attr')->insert($data);
        }else{
            $re=Db::table('member_resource_attr')->where('id', $data['id'])->update($data);
        }
        if($re!==false){
            $this->success(__('编辑成功'), 'Ziyuan/slist');
        }else{
            $this->success(__('编辑失败'), 'Ziyuan/slist');
        }
    }


    public function sdelete(){
        $id=input('get.id');
        $re=Db::table('member_resource_attr')->where('id',$id)->delete();
        if($re){
            $this->success(__('删除成功'), 'Ziyuan/slist');
        }else{
            $this->success(__('删除失败'), 'Ziyuan/slist');
        }
    }


	//资源分类
	public function flist(){
        $list=Db::table('member_resource_cate')->paginate(12);
        $this->assign('list',$list);
        return $this->view->fetch();
	}
	public function fedit(){
        $id=input('get.id');
        $data=array();
        if($id){
            $data=Db::table('member_resource_cate')->where('id', $id)->find();
        }
        $this->assign('data',$data);
		return $this->view->fetch();
	}

    public function fsave(){
        $data=$this->request->post();
        if(empty($data['id'])){
            $re=Db::table('member_resource_cate')->insert($data);
        }else{
            $re=Db::table('member_resource_cate')->where('id', $data['id'])->update($data);
        }
        if($re!==false){
            $this->success(__('编辑成功'), 'Ziyuan/flist');
        }else{
            $this->success(__('编辑失败'), 'Ziyuan/flist');
        }
    }


    public function fdelete(){
        $id=input('get.id');
        $re=Db::table('member_resource_cate')->where('id',$id)->delete();
        if($re){
            $this->success(__('删除成功'), 'Ziyuan/flist');
        }else{
            $this->success(__('删除失败'), 'Ziyuan/flist');
        }
    }



    public function upload(){
        $file = request()->file('file');
        if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $path='/public/uploads/'.$info->getSaveName();
                    return json_encode(['code'=>1,'url'=>$path]);
                }else{
                    // 上传失败获取错误信息
                    return json_encode(['code'=>0,'msg'=>$file->getError()]);
                }
         }
    }


    public function ajaxGetPage(){
        $plate_id = input('get.plate_id') ? input('get.plate_id') : 0;
        $list =Db::table('member_page')->where(array('plate_id'=>$plate_id))->select();
        $html='';
        foreach ($list as $key => $value) {
            $html.='<option value="'.$value['id'].'">'.$value['name'].'</option>';
        }
        empty($html)?$html='<option value="0">请选择</option>':'';
        $this->success($html, '');
    }


}